package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.readAndParseXML.service.IGgcsService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwGgcsServices;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.GgcsForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 *  公共厕所审批 交换
 * @author Administrator
 *
 */
@Service
@Transactional
public class GgcsServiceImpl implements IGgcsService {
	Logger logger = (Logger) LoggerFactory.getLogger(GgcsServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwGgcsServices ywGgcsServices;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String ggcsfilepath = localResource.getString("ggcsfilepath");

	// 附件存放路径
	private final String ggcsfujianpath = localResource.getString("ggcsfujianpath");

	// 解析成功反馈xml存放路径
	private final String ggcsfeedbackPath = localResource.getString("ggcsfeedbackPath");

	// 解析异常xml存放路径
	private final String ggcsexceptionPath = localResource.getString("ggcsexceptionPath");

	// 解析后，xml备份
	private final String ggcsbackPath = localResource.getString("ggcsbackPath");

	private Map<String, String> ggcsKeyMap;// xml内容的key

	private Map<String, String> ggcsKeyMapHead;// xml头部的key

	private Map<String, Long> ggcsfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void inithwShMap() {
		// 初始化数据部分对应的map
		ggcsKeyMap = new HashMap<String, String>();	
		ggcsKeyMap.put("YSL_NO", "YSL_NO");
		ggcsKeyMap.put("approvalNo", "approvalNo");
		ggcsKeyMap.put("businessCode", "businessCode");
		ggcsKeyMap.put("sendTime", "sendTime");
		
//		ggcsKeyMap.put("SQRQ", "SQRQ");
//		ggcsKeyMap.put("ACCEPT_NO", "ACCEPT_NO");
		ggcsKeyMap.put("GCMC", "GCMC");
		ggcsKeyMap.put("GC_ADDRESS", "GC_ADDRESS");
		ggcsKeyMap.put("SQDW", "SQDW");
		ggcsKeyMap.put("SQR_ADDRESS", "SQR_ADDRESS");
		ggcsKeyMap.put("SQR_TEL", "SQR_TEL");
		ggcsKeyMap.put("YZBM", "YZBM");
		ggcsKeyMap.put("JGBMSFZ", "JGBMSFZ");
		ggcsKeyMap.put("ICBM", "ICBM");
		ggcsKeyMap.put("WTR", "WTR");
		ggcsKeyMap.put("BG_TEL", "BG_TEL");
		ggcsKeyMap.put("LXDH_PHONE", "LXDH_PHONE");
		ggcsKeyMap.put("YWNR", "YWNR");
		ggcsKeyMap.put("JZXS", "JZXS");
		ggcsKeyMap.put("JZMJ", "JZMJ");
		ggcsKeyMap.put("JZDJ", "JZDJ");
		ggcsKeyMap.put("GLQY", "GLQY");
		ggcsKeyMap.put("SQNRSM", "SQNRSM");
		ggcsKeyMap.put("SQDW_GZRQ", "SQDW_GZRQ");
		
//		ggcsKeyMap.put("QYBH", "QYBH");
//		ggcsKeyMap.put("CBYW", "CBYW");
		
		
		
		
		
//		ggcsKeyMap.put("ACCEPT_NO", "ACCEPT_NO");
//		ggcsKeyMap.put("SJR", "SJR");
//		ggcsKeyMap.put("SARQ", "SARQ");
//		ggcsKeyMap.put("CBYW", "CBYW");
//		ggcsKeyMap.put("CBBM", "CBBM");
//		// ggcsKeyMap.put("会办部门", "会办部门");
//		ggcsKeyMap.put("CPSJ", "CPSJ");
//		ggcsKeyMap.put("DFSJ", "DFSJ");
//		ggcsKeyMap.put("SQDW", "SQDW");
		
		

		// 初始化头部对应的map
		ggcsKeyMapHead = new HashMap<String, String>();
		ggcsKeyMapHead.put("sn", "sn");
		ggcsKeyMapHead.put("sender", "sender");
		// ggcsKeyMapHead.put("receiver", "receiver");
		ggcsKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		ggcsfujianMap = new HashMap<String, Long>();
		ggcsfujianMap.put("FJ_01", 340l);
		ggcsfujianMap.put("FJ_02", 341l);
		ggcsfujianMap.put("FJ_03", 342l);
		ggcsfujianMap.put("FJ_04", 343l);
		ggcsfujianMap.put("FJ_05", 344l);
		ggcsfujianMap.put("FJ_06", 345l);
		ggcsfujianMap.put("FJ_07", 346l);
		ggcsfujianMap.put("FJ_08", 347l);
		ggcsfujianMap.put("FJ_09", 348l);
	}

	public void readXml(String projectPath) {
		if (ggcsKeyMap == null)
			this.inithwShMap();
		File file = new File(ggcsfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(ggcsKeyMap, filePath,
								ggcsfeedbackPath, ggcsexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(ggcsKeyMapHead,
								files[i], ggcsfeedbackPath, ggcsexceptionPath);

						// 把数据部分保存到数据库
//						YwHwShSqbForm form = new YwHwShSqbForm();
						GgcsForm form = new GgcsForm();
						for (String mapKey : ggcsKeyMap.keySet()) {
							// String value =
							// parseXMLService.getMapValue(ggcsKeyMap.get(mapKey));

							// TODO 把值放到form中
							form.setGcmc(parseXMLService.getMapValue(ggcsKeyMap
									.get("GCMC")));	
//							form.setSqdw(parseXMLService.getMapValue(ggcsKeyMap
//									.get("JSDW")));
							System.out.println("GCMC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+parseXMLService.getMapValue(ggcsKeyMap
									.get("GCMC")));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(ggcsKeyMap
									.get("GCMC")),"ccgbggcs");
							if(ywComQyxxForm != null){
								form.setQybh(ywComQyxxForm.getId().toString());
							}
							
//							String SQRQTemp = parseXMLService
//									.getMapValue(ggcsKeyMap.get("SQRQ"));
//							Date SQRQ = null;
//							if (org.apache.commons.lang.StringUtils
//									.isNotEmpty(SQRQTemp)) {
//								SQRQ = DateUtils.stringToDate(
//										SQRQTemp, DateUtils.YYYY_MM_DD);
//								form.setSqrq(SQRQ);
//							}
							
							form.setSarq(new Date());
							
							form.setGcAddress(parseXMLService.getMapValue(ggcsKeyMap
									.get("GC_ADDRESS")));
							form.setSqdw(parseXMLService.getMapValue(ggcsKeyMap
									.get("SQDW")));
							form.setSqrAddress(parseXMLService.getMapValue(ggcsKeyMap
									.get("SQR_ADDRESS")));
							form.setSqrTel(parseXMLService.getMapValue(ggcsKeyMap
									.get("SQR_TEL")));
							form.setYzbm(parseXMLService.getMapValue(ggcsKeyMap
									.get("YZBM")));
							form.setJgbmsfz(parseXMLService.getMapValue(ggcsKeyMap
									.get("JGBMSFZ")));							
							form.setIcbm(parseXMLService.getMapValue(ggcsKeyMap
									.get("ICBM")));
							form.setWtr(parseXMLService.getMapValue(ggcsKeyMap
									.get("WTR")));
							form.setBgTel(parseXMLService.getMapValue(ggcsKeyMap
									.get("BG_TEL")));
							form.setLxdhPhone(parseXMLService.getMapValue(ggcsKeyMap
									.get("LXDH_PHONE")));							
							form.setYwnr(parseXMLService.getMapValue(ggcsKeyMap
									.get("YWNR")));
							form.setJzxs(parseXMLService.getMapValue(ggcsKeyMap
									.get("JZXS")));
							
							String JZMJTemp = parseXMLService
							.getMapValue(ggcsKeyMap.get("JZMJ"));
							if (org.apache.commons.lang.StringUtils
									.isNotEmpty(JZMJTemp)) {
								form.setJzmjs(JZMJTemp);
							}
							
							form.setJzdj(parseXMLService.getMapValue(ggcsKeyMap
									.get("JZDJ")));
							
							form.setGlqy(parseXMLService.getMapValue(ggcsKeyMap
									.get("GLQY")));
							form.setSqnrsm(parseXMLService.getMapValue(ggcsKeyMap
									.get("SQNRSM")));
							String sqdwGzrqTemp = parseXMLService
									.getMapValue(ggcsKeyMap.get("SQDW_GZRQ"));
							Date SQDW_GZRQ = null;
							if (org.apache.commons.lang.StringUtils
									.isNotEmpty(sqdwGzrqTemp)) {
								SQDW_GZRQ = DateUtils.stringToDate(
										sqdwGzrqTemp, DateUtils.YYYY_MM_DD);
								form.setSqdwGzrq(SQDW_GZRQ);
							}
							
							form.setCbyw("拆除、关闭公厕许可");
							form.setYsl(1l);
							form.setSendNotice("Y");
							form.setYslNo(parseXMLService.getMapValue(ggcsKeyMap
									.get("YSL_NO")));
							form.setApprovalNo(parseXMLService.getMapValue(ggcsKeyMap
									.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(ggcsKeyMap
									.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(ggcsKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(
										wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
						}
						// 调用DAO，保存到数据库
						try {
							ywGgcsServices.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									ggcsKeyMapHead, ggcsfeedbackPath, ggcsexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, ggcsfujianpath, ggcsfeedbackPath, ggcsexceptionPath, ggcsKeyMapHead,
								files, ggcsfujianMap, i, Integer.parseInt(form.getQybh()), form.getId(), "ccgbggcs");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								ggcsKeyMapHead, ggcsfeedbackPath, ggcsexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, ggcsbackPath + fileName,
							fileName);
				}
			}
		}
	}

}
